$(document).ready(function(e) {
	createPageTab();
	createProductListTab();
	
	readyAllBrand("brandSelect");
	if($("#brandId").val()!=null && $("#brandId").val()!=""){
		$("#brandSelect").val($("#brandId").val());
	}
	
	findProductList(1);
});

/**
 * 读取所有品牌,将品牌信息set进select
 */
function readyAllBrand(divId){
	var tmp2=root+"/admin/brand!getBrandAll";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var htmlStr = '';
			for(var i=0; i<json.length; i++){
				htmlStr += '<option value="'+json[i].id+'">'+json[i].name+'</option>';
			}
			$("#"+divId).append(htmlStr);
		}
	});
}

var totalPageNum = 0;
var isDesc = "desc";
var productList = null;
function findProductList(pageNow){
	var tmp2=root+"/admin/product!findProductByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "brand.id":$("#brandSelect").val(), "product.name": $("#productName").val()},
		dataType:"json",
		success: function(json){
			if(productList!=null) createProductListTab();
			totalPageNum = json.totalPageNum;
			productList = json.pageData;
			
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].brand.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].barcode+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].xiangGui+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].pinLei+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateProductDiv('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteProduct('+json.pageData[i].id+','+json.curPageNum+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findProductList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findProductList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findProductList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findProductList(json.totalPageNum);});
		}
	});
}

function showAddProductDiv(){
	if($("#productBrandSelect").html()==""){
		readyAllBrand("productBrandSelect");
	}
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAddProductDiv(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function saveProduct(){
	if($("#productName_add").val()!="" && $("#productName_add").val()!=""){
		$("#addProductForm").submit();
	}else{
		alert("商品名不能为空！");
	}
}

function deleteProduct(productId, pageNow){
	var ids = "";
	if(productId==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个商品！");
			return;
		}
	}
	
	var tmp2=root+"/admin/product!deleteProduct";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "ids":ids, "product.id": productId, "isDesc": isDesc, "brand.id":$("#brandSelect").val(), "product.name": $("#productName").val()},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findProductList(json.curPageNum);
		}
	});
}

function showUpdateProductDiv(pageNow, index){
	if($("#productBrandSelect_u").html()==""){
		readyAllBrand("productBrandSelect_u");
	}
	$("#backGroud").show();
	$("#updateDiv").show();
	$("#productBrandSelect_u").val(productList[index].brand.id);
	$("#productId_u").val(productList[index].id);
	$("#productName_u").val(productList[index].name);
	$("#productBarcode_u").val(productList[index].barcode);
	$("#productXiangGui_u").val(productList[index].xiangGui);
	$("#productPinLei_u").val(productList[index].pinLei);
	
	$("#updateProductButton").bind("click",function(){updateProduct(pageNow);});
}

function closeUpdateProductDiv(){
	$("#updateProductForm")[0].reset();
	$("#updateProductButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function updateProduct(pageNow){
	var tmp2=root+"/admin/product!updateProduct";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"product.id":$("#productId_u").val(),"product.name":$("#productName_u").val(),"brand.id":$("#productBrandSelect_u").val(),
				"product.barcode": $("#productBarcode_u").val(), "product.xiangGui": $("#productXiangGui_u").val(), "product.pinLei": $("#productPinLei_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateProductDiv();
			if(json.code=="y"){
				findProductList(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findProductList(p);
	}
}